package com.njupt.user.controller;

import com.njupt.user.model.User;
import com.njupt.user.service.GradeService;
import com.njupt.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ASUS on 2019/4/29.
 */
@Controller
public class GradeController {
    @Autowired
    private UserService userService;
    @Autowired
    private GradeService gradeService;
    @RequestMapping("checkwork")
    public ModelAndView checkwork(ModelAndView mv,
                                    HttpSession session){
        List<User> list = userService.findAll();
        for(int i =0;i<list.size();i++){
            User user =  list.get(i);
            if (user.getUsertype().equals("老师")){
                list.remove(i);
            }
        }
        session.setAttribute("users",list);
        mv.setViewName("fpga_grade1");
        return mv;
    }
    @RequestMapping("setgrade01")
    public ModelAndView setgrade01(ModelAndView mv,
                                   @RequestParam("username") String username,
                                  HttpSession session){
        session.setAttribute("username",username);
        mv.setViewName("fpga_show1");
        return mv;
    }
    @RequestMapping("setgrade02")
    public ModelAndView setgrade02(ModelAndView mv,
                                   @RequestParam("username") String username,
                                   @RequestParam("name_test") String name_test,
                                   HttpSession session)throws IOException{
        String file = "C:\\Users\\ASUS\\Desktop\\毕业设计\\njupt\\web\\upload\\"+username+"_"+name_test+".txt";
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuffer sb = new StringBuffer();
        String line;
        while ((line = br.readLine()) != null) {
            sb.append(line);
            sb.append("\r\n");
            // 转成char加到StringBuffer对象中
        }
        session.setAttribute("test_txt",sb.toString());
       br.close();
        session.setAttribute("username",username);
        session.setAttribute("name_test",name_test);
        mv.setViewName("fpga_setgrade1");
        return mv;
    }
    @RequestMapping("setgrade03")
    public ModelAndView setgrade03(ModelAndView mv,
                                   @RequestParam("username") String username,
                                   @RequestParam("name_test") String name_test,
                                   @RequestParam("grade") String grade,
                                   HttpSession session){
        Map<String,String> map=new HashMap<String,String>();
        map.put("username", username);
        map.put("name_test", name_test);
        map.put("grade", grade);
        gradeService.updateGrade(map);
        session.setAttribute("username",username);
        session.setAttribute("name_test",name_test);
        mv.setViewName("fpga_grade1");
        return mv;
    }
}
